#include "MainMenu.h"
#include "Utilities.h"
#include "PrintUtilities.h"
#include "StartingMenu.h"
#include "LeagueManager.h"
#include "MenuManager.h"
#include "EndLeagueMenu.h"
#include "GameOverMenu.h"
#include "UserManager.h"
#include "GameControls.h"
#include "GameSettings.h"

#include <ctime>

MainMenu::MainMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
{
	m_isDebugMode = GetSP()->GetSettings().GetValue("B_DEBUG_MODE");
}

void MainMenu::Draw() const
{
	const size_t currentTurn =  GetSP()->GetLeagueMgr().GetCurrentTurn();
	const bool isEnded = GetSP()->GetLeagueMgr().IsEnded();

	std::cout	<< Print::Title(" -SEASON " + Utilities::IntToString(GetSP()->GetLeagueMgr().GetCurrentSeason()) + "-\n  MENU:") << std::endl << std::endl;

	std::cout	<< Print::Options(	
						(isEnded ? " 1) End Season.\n" : " 1) Play Turn (" + Utilities::IntToString(currentTurn + 1) + ").\n") + 
						" 2) Team Management.\n"
						" 3) View Classifications.\n"
						" 4) View Turns and Calendar.\n"
						" 5) View Player Statistics.\n" 
						" 6) View Team Statistics.\n" 
						" 7) Save Game.\n"
						+ (m_isDebugMode ? " 8) Goto End Season. (***DEBUG MODE***)\n" : "") + 
						+ (m_isDebugMode ? "sX) Simulate X Seasons. (***DEBUG MODE***)\n" : "") + 
						"\n 0) Exit from game.");
	std::cout	<< " Selection: ";
}

bool MainMenu::Input(const std::string& i_input)
{
	bool goodInput = true;
	const bool isEnded = GetSP()->GetLeagueMgr().IsEnded();

	int res = 0;
	int yearsToSimulate = 0;
	if(i_input[0] == 's')
	{
		res = 1234567;
		yearsToSimulate = Utilities::StringToInt(i_input.c_str() + 1);
	}
	else
	{
		res = Utilities::StringToInt(i_input);
	}

	switch (res)
	{
		case 1:
			if(!isEnded)
			{
				GetMM()->GetGameControls()->Simulate(false);
				GetMM()->SetMenu(GetMM()->GetPlayTurnsMenu());	
			}
			else
			{
				std::stringstream message;
				GetMM()->GetGameControls()->EndSeason(false, message);
				EndLeagueMenu* elMenu = static_cast<EndLeagueMenu*>(GetMM()->GetEndLeagueMenu());
				elMenu->SetMessage(message.str());
				GetMM()->SetMenu(elMenu);

				GetSP()->GetUserMgr().CheckLosingPlayers();
				if(!GetSP()->GetUserMgr().m_gameoverNames.empty())
					GetMM()->PushMenu(GetMM()->GetGameOverMenu());
			}
			break;

		case 2:
			GetMM()->SetMenu(GetMM()->GetTeamsManagementMenu());			
			break;

		case 3:
			GetMM()->SetMenu(GetMM()->GetClassificationMenu());
			break;

		case 4:
			GetMM()->SetMenu(GetMM()->GetTurnsMenu());
			break;
		
		case 5:
			GetMM()->SetMenu(GetMM()->GetPlayersStatsMenu());
			break;

		case 6:
			GetMM()->SetMenu(GetMM()->GetTeamsStatsMenu());
			break;

		case 7:
			GetMM()->SetMenu(GetMM()->GetSaveMenu());
			break;

		case 8:
			if(m_isDebugMode)
				while(!GetSP()->GetLeagueMgr().IsEnded())
					GetMM()->GetGameControls()->Simulate(true);
			goodInput = false;
			break;

		case 1234567:
			if(m_isDebugMode)
			{
				std::stringstream message;
				while(yearsToSimulate != 0)
				{
					clock_t clock0 = clock();
					while(!GetSP()->GetLeagueMgr().IsEnded())
						GetMM()->GetGameControls()->Simulate(true);
					GetMM()->GetGameControls()->EndSeason(true, message);
					--yearsToSimulate;
					std::cout << "Season: " << GetSP()->GetLeagueMgr().GetCurrentSeason() << " - Time Elapsed: " << clock() - clock0 << std::endl;
				}
			}
			goodInput = false;
			break;

		case 0:
			{
				StartingMenu* startingMenu = static_cast<StartingMenu*>(GetMM()->GetStartingMenu());
				startingMenu->m_continueGame = true;
				GetMM()->SetMenu(startingMenu);
			}
			break;

		default:
			goodInput = false;
			break;
	}
	return goodInput;
}